﻿using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage.Repost;
using ProduceBasicData.Api.DTO.ProductionManage;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.Domain.Inventories;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage.Repost
{
    public class GetReportCommandHandler : IRequestHandler<GetReportCommand, ApiPaging<ShowReportdto>>
    {

        private readonly IBaseRespository<Work_order> _work_order;
        private readonly IBaseRespository<Production_report> _production_report;
        private readonly IBaseRespository<ProductMange> _productmange;
        private readonly IBaseRespository<CraftModel> _craftModel;

        public GetReportCommandHandler(IBaseRespository<Work_order> work_order,
            IBaseRespository<Production_report> production_report,
            IBaseRespository<ProductMange> productmange,
            IBaseRespository<CraftModel> craftModel)
        {
            _work_order = work_order;
            _production_report = production_report;
            _productmange = productmange;
            _craftModel = craftModel;
        }

        public Task<ApiPaging<ShowReportdto>> Handle(GetReportCommand request, CancellationToken cancellationToken)
        {
             var result=new ApiPaging<ShowReportdto>()
             {
                 Code=ApiEnum.Success,
                 Msg="查询报工信息成功"
             };
            
            // 使用左连接避免因关联表数据缺失导致查询失败
            var info = from a in _production_report.GetAll()
                       join b in _work_order.GetAll() on a.Order_id equals b.Id
                       from c in _craftModel.GetAll().Where(p => p.Id == a.Process_id).DefaultIfEmpty()
                       from d in _productmange.GetAll().Where(p => p.Id == a.Product_id).DefaultIfEmpty()
                       where !a.Isdel && !b.Isdel
                       select new ShowReportdto()
                       {
                           Report_no = a.Report_no,
                           Id = a.Id,
                           Isdel = b.Isdel,
                           Order_id = a.Order_id,
                           Order_no = b.Order_no,
                           Process_id = a.Process_id,
                           CraftNo = c != null ? c.CraftNo : "",
                           CraftName = c != null ? c.CraftName : "",
                           Product_id = a.Product_id,
                           Product_no = d != null ? d.Product_no : "",
                           Product_name = d != null ? d.Product_name : "",
                           Production_time = a.Production_time,
                           Plan_qty = a.Plan_qty,
                           Report_status = a.Report_status,
                           StartTime = a.StartTime,
                           EndTime = a.EndTime,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                       };

            // 添加查询条件过滤
            if (request.OrderId > 0)
            {
                info = info.Where(x => x.Order_id == request.OrderId);
            }
            if (!string.IsNullOrEmpty(request.Report_no))
            {
                info = info.Where(x => x.Report_no.Contains(request.Report_no));
            }
            if (!string.IsNullOrEmpty(request.Order_no))
            {
                info = info.Where(x => x.Order_no.Contains(request.Order_no));
            }
            if (!string.IsNullOrEmpty(request.CraftNo))
            {
                info = info.Where(x => x.CraftNo.Contains(request.CraftNo));
            }
            if (!string.IsNullOrEmpty(request.CraftName))
            {
                info = info.Where(x => x.CraftName.Contains(request.CraftName));
            }
            if (!string.IsNullOrEmpty(request.Product_no))
            {
                info = info.Where(x => x.Product_no.Contains(request.Product_no));
            }
            if (!string.IsNullOrEmpty(request.Product_name))
            {
                info = info.Where(x => x.Product_name.Contains(request.Product_name));
            }
            if (request.Report_status > 0)
            {
                info = info.Where(x => x.Report_status == request.Report_status);
            }

            result.TotalCount = info.Count();
            result.PageDatas=info.OrderByDescending(o=>o.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();

            return Task.FromResult(result);
        }
    }
}
